import os

from flask_admin import Admin
from flask_admin.contrib import rediscli
from flask_admin.contrib.fileadmin import FileAdmin
from flask_admin.menu import MenuLink
from redis import Redis

from app.database.models import (
    Account,
    Document,
    IdAccount,
    Statistics,
    Task,
    Unique,
    User,
    Whitepage,
)
from app.server.flask_app import db
from app.server.flask_app.model_views.AccountModelView import AccountModelView
from app.server.flask_app.model_views.AdminModelView import AdminModelView
from app.server.flask_app.model_views.DocumentModelView import DocumentModelView
from app.server.flask_app.model_views.HomeView import HomeView
from app.server.flask_app.model_views.IdAccountModelView import IdAccountModelView
from app.server.flask_app.model_views.StatsModelView import StatsModelView
from app.server.flask_app.model_views.TaskModelView import TaskModelView
from app.server.flask_app.model_views.UniqueModelView import UniqueModelView

# from app.server.flask_app.model_views.StatsView import StatsView
from app.server.flask_app.model_views.UserModelView import UserModelView
from app.server.flask_app.model_views.WhitepageModelView import WhitepageModelView


def init_admin_panel(app):
    admin = Admin(
        app,
        name="Admin panel",
        template_mode="bootstrap3",
        index_view=HomeView(
            name="Home", menu_icon_type="glyph", menu_icon_value="glyphicon-home"
        ),
    )
    admin.add_view(UserModelView(User, db.session, name="Users", category="Models"))
    admin.add_view(TaskModelView(Task, db.session, name="Tasks", category="Models"))
    admin.add_view(
        DocumentModelView(Document, db.session, name="Documents", category="Models")
    )
    admin.add_view(
        AccountModelView(Account, db.session, name="Accounts", category="Models")
    )
    admin.add_view(
        UniqueModelView(Unique, db.session, name="Unique", category="Models")
    )
    admin.add_view(
        WhitepageModelView(Whitepage, db.session, name="Whitepages", category="Models")
    )
    admin.add_view(
        IdAccountModelView(IdAccount, db.session, name="Id Accounts", category="Models")
    )
    admin.add_view(
        StatsModelView(Statistics, db.session, name="Stats", category="Models")
    )
    admin.add_view(AdminModelView(db.session, category="Management"))
    admin.add_view(
        FileAdmin(os.getcwd(), "bot", name="Static Files", category="Management")
    )
    admin.add_view(rediscli.RedisCli(Redis(), category="Management"))
    admin.add_link(
        MenuLink(name="Logout", class_name="nav-item", endpoint="auth_bp.logout")
    )
